package com.ruoyi.project.system.coordinates.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.RedisOps;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.monitor.job.domain.Point3D;
import com.ruoyi.project.monitor.job.task.GetPointSourceJob;
import com.ruoyi.project.system.coordinates.domain.Point3DTools;
import com.ruoyi.project.system.coordinates.domain.TbCoordinates;
import com.ruoyi.project.system.coordinates.service.ITbCoordinatesService;
import com.ruoyi.project.system.sending.domain.TbPlcSending;
import com.ruoyi.project.system.sending.service.ITbPlcSendingService;
import com.ruoyi.project.system.sending.service.impl.TbPlcSendingServiceImpl;
import com.ruoyi.project.system.setting.service.ITbParamSettingService;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 实时坐标Controller
 * 
 * @author kangjie
 * @date 2019-12-01
 */
@Controller
@RequestMapping("/system/coordinates")
public class TbCoordinatesController extends BaseController
{
    private String prefix = "system/coordinates";

    @Autowired
    private ITbParamSettingService tbParamSettingService;
    @Autowired
    GetPointSourceJob getPointSourceJob;
    @Autowired
    ITbPlcSendingService tbPlcSendingService;

    @RequiresPermissions("system:coordinates:view")
    @GetMapping()
    public String coordinates()
    {
        return prefix + "/coordinates";
    }

    /**
     * 设置执行是否ok
     */
    @GetMapping("/getLmsIsRunningOk")
    @ResponseBody
    public String getLmsIsRunningOk(TbCoordinates tbCoordinates)
    {
//            Point3D[] point3DArr = (Point3D[]) RedisOps.getObject("test");
            String bo  = getPointSourceJob.getLmsIsRunningOk();
            return bo;
    }

    /**
     * 设置云台参数
     * @return
     */
    @GetMapping("/getLmsSetScanParameter")
    @ResponseBody
    public String getLmsSetScanParameter(){
        String string = getPointSourceJob.getLmsSetScanParameter();
        return string;
    }

    /**
     * 云台开始扫描
     * @return
     */
    @GetMapping("/getLmsStart3DScanOnce")
    @ResponseBody
    public String getLmsStart3DScanOnce(){
        String string = getPointSourceJob.getLmsStart3DScanOnce();
        return string;
    }

    /**
     * 获取点云数据
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/getLms3DPointCloud")
    @ResponseBody
    public Point3DTools getLms3DPointCloud() throws InterruptedException {
        Thread.sleep(20000);
        Point3DTools point3DTools = getPointSourceJob.getLms3DPointCloud();
        return point3DTools;
    }

    /**
     * 获取设备使用参数
     * @return
     */
    @GetMapping("/getParam")
    @ResponseBody
    public Object getParam(){
        return tbParamSettingService.getParam();
    }

    /**
     * 自动处理点云数据
     * @return
     */
    @PostMapping("/autoDeal")
    @ResponseBody
    public String autoDeal(@RequestBody String json){
        List<Object> list = JSONArray.parseArray(json,Object.class);
        int size = list.size();
        System.out.println("size()"+size);
        List<Point3D> point3DList = new ArrayList<>();
        for (int i = 0 ; i<list.size();i++){
            JSONArray array = (JSONArray) list.get(i);
            Point3D point3D = new Point3D();
            point3D.setX(array.getFloat(0));
            point3D.setY(array.getFloat(1));
            point3D.setZ(array.getFloat(2));
            point3DList.add(point3D);
        }
        String string = tbParamSettingService.autoDeal(point3DList);
        return string;
    }

    /**
     * 保存plc发送的状态
     * @param fOpen
     * @param startStatus
     * @param model
     * @return
     */
    @PostMapping("/saveStatus")
    @ResponseBody
    public String saveStatus(String startStatus,String model,
    		String count,String error,
    		String is_complete){
        String result = tbParamSettingService.
        		savePlcStatus(startStatus,model,count,error,is_complete);
        return result;
    }

    /**
     * 获取登录span框信息
     * @return
     */
    @GetMapping("/getLoginCount")
    @ResponseBody
    public String getLoginCount(){
        Map<String,Long> resultMap = tbParamSettingService.getLoginDayCount();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(resultMap);

        return jsonArray.toString();
    }
    /**
     * 获取扫描scan框信息
     * @return
     */
    @GetMapping("/getScanCount")
    @ResponseBody
    public String getScanCount(){
        Map<String,Long> resultMap = tbParamSettingService.getScanDayCount();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(resultMap);

        return jsonArray.toString();
    }
    /**
     * 获取自动操作次数框信息
     * @return
     */
    @GetMapping("/getAutoLoginCount")
    @ResponseBody
    public String getAutoLoginCount(){
        Map<String,Long> resultMap = tbParamSettingService.getAutoLoginDayCount();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(resultMap);

        return jsonArray.toString();
    }
    /**
     * 获取半自动操作次数框信息
     * @return
     */
    @GetMapping("/getSemiAutoOperationCount")
    @ResponseBody
    public String getSemiAutoOperationCount(){
        Map<String,Long> resultMap = tbParamSettingService.getSemiAutoOperationDayCount();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(resultMap);

        return jsonArray.toString();
    }


    /**
     * 保存半自动数据
     * @param message
     * @return
     */
    @PostMapping("/saveAutoMatic")
    @ResponseBody
    public String saveAutoMatic(String message){
        TbPlcSending tbPlcSending = new TbPlcSending();
        tbPlcSending.setFlag(1);//半自动
        tbPlcSending.setInfo(message);
        tbPlcSendingService.insertTbPlcSending(tbPlcSending);
        return "success";
    }
}
